package com.xpjhs.user.web.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;





import com.xpjhs.user.model.SexType;
import com.xpjhs.user.model.User;
import com.xpjhs.user.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;


	
	@RequestMapping("/profile")
	public String profile(ModelMap modelMap) {
		modelMap.addAttribute("SexType", SexType.values());
		return "user/profile";
	}

	@RequestMapping("/usernameIsExist")
	public @ResponseBody
	boolean usernameIsExist(String username) {
		return userService.usernameIsExist(username);
	}

	@RequestMapping("/importUser")
	public String importUser(User user) {
		try {
			userService.importUser(user);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/test")
	@ResponseBody
	public ArrayList<Object> test() {
		ArrayList<Object> citys = new ArrayList<Object>();
		List<String> list = userService.list();
		for (String city : list) {
			citys.add("{name:'" + city + "',value:"
					+ userService.cityCount(city).size() + "}");
		}
		System.out.println(citys.toString());
		return citys;
	}
	

}
